<template>
  <div class="app-container">
    <div style="margin-top:30px;">
      <div>
        <span v-permission="['ADMIN']" class="permission-alert">
          Only
          <el-tag class="permission-tag" size="small">ADMIN</el-tag> can see this
        </span>
        <el-tag v-permission="['ADMIN']" class="permission-sourceCode" type="info">
          v-permission="['ADMIN']"
        </el-tag>
      </div>

      <div>
        <span v-permission="['editor']" class="permission-alert">
          Only
          <el-tag class="permission-tag" size="small">editor</el-tag> can see this
        </span>
        <el-tag v-permission="['editor']" class="permission-sourceCode" type="info">
          v-permission="['editor']"
        </el-tag>
      </div>

      <div>
        <span v-permission="['ADMIN','editor']" class="permission-alert">
          Both
          <el-tag class="permission-tag" size="small">ADMIN</el-tag> and
          <el-tag class="permission-tag" size="small">editor</el-tag> can see this
        </span>
        <el-tag v-permission="['ADMIN','editor']" class="permission-sourceCode" type="info">
          v-permission="['ADMIN','editor']"
        </el-tag>
      </div>
    </div>

    <div key="'checkPermission1'" style="margin-top:60px;">
      <aside>
        In some cases, using v-permission will have no effect. For example: Element-UI's Tab component or el-table-column and other scenes that dynamically render dom. You can only do this with v-if.
        <br> e.g.
      </aside>

      <el-tabs type="border-card" style="width:550px;">
        <el-tab-pane v-if="checkPermission(['ADMIN'])" label="Admin">
          Admin can see this
          <el-tag class="permission-sourceCode" type="info">
            v-if="checkPermission(['ADMIN'])"
          </el-tag>
        </el-tab-pane>

        <el-tab-pane v-if="checkPermission(['editor'])" label="Editor">
          Editor can see this
          <el-tag class="permission-sourceCode" type="info">
            v-if="checkPermission(['editor'])"
          </el-tag>
        </el-tab-pane>

        <el-tab-pane v-if="checkPermission(['ADMIN','editor'])" label="Admin-OR-Editor">
          Both ADMIN or editor can see this
          <el-tag class="permission-sourceCode" type="info">
            v-if="checkPermission(['ADMIN','editor'])"
          </el-tag>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import permission from '@/directive/permission/index' // 权限判断指令
import checkPermission from '@/utils/checkPermission' // 权限判断函数

export default {
  name: 'DirectivePermission',
  components: {},
  directives: { permission },
  data() {
    return {
    }
  },
  methods: {
    checkPermission
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  ::v-deep .permission-alert {
    width: 320px;
    margin-top: 15px;
    background-color: #f0f9eb;
    color: #67c23a;
    padding: 8px 16px;
    border-radius: 4px;
    display: inline-block;
  }
  ::v-deep .permission-sourceCode {
    margin-left: 15px;
  }
  ::v-deep .permission-tag {
    background-color: #ecf5ff;
  }
}
</style>

